package io.bhex.baselib.utils;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * ================================================
 * 作   者：ppzhao
 * 创建时间：2020-04-28
 * 邮   箱：
 * 描   述：
 * ================================================
 */

public class SignUtils {
    /**
     * 加密算法
     */
    private static final String AES_ALGORITHM = "AES";

    /**
     * 加密算法/加密模式/填充类型
     * 本例采用AES加密，ECB加密模式，PKCS5Padding填充
     */
    private static final String AES_CIPHER_MODE = "AES/ECB/PKCS5Padding";

    public static String encryptDataWithAES(String key, String data) throws Exception {
        return encryptDataWithAES(key, data.getBytes(Charsets.UTF_8));
    }

//    public static String encryptDataWithAES(String key, byte[] data) throws Exception {
//        byte[] keyBytes = Hashing.sha256().hashString(key, Charsets.UTF_8).asBytes();
//        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
//        Cipher cipher = Cipher.getInstance(AES_CIPHER_MODE);
//        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
//        byte[] cipherTextBytes = cipher.doFinal(data);
//        return android.util.Base64.encodeToString(cipherTextBytes, 0);
////        return Base64.getEncoder().encodeToString(cipherTextBytes);
//    }

    public static String decryptDataWithAES(String key, String data) throws Exception {
//        return decryptDataWithAES(key, Base64.getDecoder().decode(data));
        return decryptDataWithAES(key, android.util.Base64.decode(data, 0));
    }

//    public static String decryptDataWithAES(String key, byte[] data) throws Exception {
//        byte[] keyBytes = Hashing.sha256().hashString(key, Charsets.UTF_8).asBytes();
//        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
//        Cipher cipher = Cipher.getInstance(AES_CIPHER_MODE);
//        cipher.init(Cipher.DECRYPT_MODE, keySpec);
//        byte[] cipherTextBytes = cipher.doFinal(data);
//        return new String(cipherTextBytes, Charsets.UTF_8);
//    }

    /**
     * NEW
     ***********/
    public static String decryptDataWithAES(String key, byte[] data) throws Exception {
//        byte[] keyBytes = Hashing.sha256().hashString(key, Charsets.UTF_8).asBytes();
        byte[] keyBytes = Hashing.md5().hashString(key, Charsets.UTF_8).toString().substring(8, 24).getBytes();
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
        Cipher cipher = Cipher.getInstance(AES_CIPHER_MODE);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] cipherTextBytes = cipher.doFinal(data);
        return new String(cipherTextBytes, Charsets.UTF_8);
    }

    public static String encryptDataWithAES(String key, byte[] data) throws Exception {
//        byte[] keyBytes = Hashing.sha256().hashString(key, Charsets.UTF_8).asBytes();
        byte[] keyBytes = Hashing.md5().hashString(key, Charsets.UTF_8).toString().substring(8, 24).getBytes();
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
        Cipher cipher = Cipher.getInstance(AES_CIPHER_MODE);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] cipherTextBytes = cipher.doFinal(data);
        return org.apache.commons.codec.binary.Base64.encodeBase64String(cipherTextBytes);
//        return Base64.getEncoder().encodeToString(cipherTextBytes);
//        return android.util.Base64.encodeToString(cipherTextBytes, 0);
    }

}
